<?php
/**
 * User: Tegic
 * Date: 2018/6/13
 * Time: 09:47
 */
namespace app\workerman;
use GatewayWorker\Lib\Gateway;
use think\facade\Log;
use think\Db;
use think\facade\Config;
use redis\Redis;
class Events
{
    /**修改业务逻辑 需要重启workerman**/
    static $num = 0;
    public static function test(){
        return "HALO";
    }
    public static function onWorkerStart($businessWorker)
    {
        Log::info("onWorkerStart");
    }
    
    public static function onConnect($client_id)
    {
        global $num;
        $redata = [
            'type' => 'init',
            'client_id'  => $client_id
        ];
        Gateway::sendToClient($client_id,tojson(['code'=>200,'data'=>$redata]));
    }
    public static function onWebSocketConnect($client_id, $data)
    {
      Log::info("onWebSocketConnect");  
    }
    public static function onMessage($client_id, $message)
    {
        $dbconf = Config::get('vister_db');
        $redis = new Redis();

        $data = json_decode($message,true);

        if($data['type'] == 'bind'){
            $uinfo = json_decode($redis::hget('dev_pc_index', $client_id),true);
            if(!$uinfo){ Gateway::sendToClient($data['client_id'], tojson(['code'=>'400','data'=>[],'message'=>'连接失败']));return;}

            Gateway::bindUid($data['client_id'], $uinfo['auid']);

            $redata = [
                'tips' => "获取最新消息",
                'time' => date('Y-m-d H:i:s',time()),
                'type' => 'indexdata',
                're_data' => 'false'
            ];
            Gateway::sendToClient($data['client_id'], tojson(['code'=>'200','data'=>$redata]));
        }
        //首页数据
        if($data['type']=='indexdata'){
            //最新数据
            $uid = Gateway::getUidByClientId($client_id);
            $redata = [
                'tips' => "获取最新消息",
                'time' => date('Y-m-d H:i:s',time()),
                'type' => 'indexdata',
            ];
            $re_data = $redis::get('wss_client_id'.$client_id.'_repdata');
            $uinfo = json_decode($redis::hget('dev_pc_index', $client_id),true);
    
            try{
                switch ($uinfo['authorize']) {
                    case 1:
                        //$redata['count_company'] = Db::connect($dbconf)->table('view_company')->count();
                        $redata['historydata'] =  Db::connect($dbconf)->table('view_sysdata')
                            ->find();
                        # code...
                        break;
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                        $redata['count_company'] = 1;
                        # code...
                        break;
                    case 8:
                        $new_uinfo = Db::connect($dbconf)->table('view_user')->where('id = '.$uinfo['buid'])->find();
                        $redata['count_company'] = Db::connect($dbconf)->table('view_company')->where('property_id = '.$new_uinfo['companyid'])->count();
                        // $in_company_id = array_column($in_company, 'id');
                        // $redata['count_company'] = Db::connect($dbconf)->table('view_company')->where('c')->count();
                        # code...
                        break;
                    default:
                        # code...
                        break;
                }
                
            }catch(\Throwable $th){
                $redata['count_company'] = NULL;
            }
            
            $redata['re_data'] = $re_data;
            Gateway::sendToCurrentClient(tojson(['code'=>200,'data'=>$redata]));
        }
        if($data['type']=='visitorindata'){
            //最新数据
            $uid = Gateway::getUidByClientId($client_id);
            $redata = [
                'tips' => "获取最新消息",
                'time' => date('Y-m-d H:i:s',time()),
                'type' => 'indexdata',
            ];
            $re_data = $redis::get('wss_client_id'.$client_id.'_repdata');
            $uinfo = json_decode($redis::hget('dev_pc_index', $client_id),true);
    
            try{
                switch ($uinfo['authorize']) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 8:
                        //$redata['count_company'] = NULL;
                        break;
                    default:
                        # code...
                        break;
                }
                
            }catch(\Throwable $th){
                //$redata['count_company'] = NULL;
            }
            
            $redata['re_data'] = $re_data;

            //访客数据业务逻辑
            $nowdaytime = strtotime(date('Y-m-d', time()));
            $where = '';
            $redata['data'] = Db::connect($dbconf)->table('view_new_app_meeting_info_copy_date')->where($where)
                                ->where('appointment_status not in (2,3) and auditing = 1')
                                ->where('app_visittime','between',[$nowdaytime,$nowdaytime+24*60*60])
                                ->order('app_visittime desc')
                                ->select();

            Gateway::sendToCurrentClient(tojson(['code'=>200,'data'=>$redata]));
        }

        if($data['type']=='paystatus'){
            //最新数据
            $uid = Gateway::getUidByClientId($client_id);
            $redata = [
                'tips' => "获取最新消息",
                'time' => date('Y-m-d H:i:s',time()),
                'type' => 'indexdata',
            ];
            $re_data = $redis::get('wss_client_id'.$client_id.'_repdata');
            $uinfo = json_decode($redis::hget('dev_pc_index', $client_id),true);
    
            
            $redata['re_data'] = $re_data;

            //访客数据业务逻辑
            $nowdaytime = strtotime(date('Y-m-d', time()));
            $where = 'tradeNo = "'.$data['tradeNo'].'"';
            $redata['data'] = Db::connect($dbconf)->table('commodity_orders')->where($where)
                                ->find();

            Gateway::sendToCurrentClient(tojson(['code'=>200,'data'=>$redata]));
        }

        // $redata = [
        //     'tips'=> "普通消息"
        // ];
        // Gateway::sendToCurrentClient(tojson(['code'=>200,'data'=>$redata]));
    }
    //关闭连接 清除用户关联信息
    public static function onClose($client_id)
    {
        $redis = new Redis();
        $uid = Gateway::getUidByClientId($client_id);
        try{
            Gateway::unbindUid($client_id, $uid);
            $redis::del('wss_client_id'.$client_id.'_repdata');
            $redis::hdel('dev_pc_index',$client_id);
        }catch(\Throwable $th){
            Log::info("error-wss-close".$uid);
        }
        
    }
}